package com.sg.hms.controller;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sg.hms.Model.Equiptment;
import com.sg.hms.Service.RoomService;
import com.sg.hms.ServiceImpl.RoomServiceImpl;

import net.sf.json.JSONArray;

/**
 * 
 * @author 吴磊
 *
 */
public class RoomEquiiptShortController extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		RoomService room=new RoomServiceImpl();
		ArrayList<Equiptment> some=room.gEquiptments();
		ArrayList<Equiptment> all=room.getAllEquiptment();
		for (Equiptment al : all) {
			for (Equiptment som : some) {
				if (al.geteId()==som.geteId()) {
				   al.setSum(al.getSum()-som.getSum());
				}
			}
		}
		ArrayList<Equiptment> need=new ArrayList<>();
		for (Equiptment equiptment : all) {
			if (equiptment.getSum()<=0) {
				need.add(equiptment);
			}
		}
		JSONArray jsonArray=JSONArray.fromObject(need);
		resp.setCharacterEncoding("UTF-8");
		Writer writer=resp.getWriter();
		writer.write(jsonArray.toString());
		writer.flush();
		writer.close();
	}

}
